﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
    public class ModelConfig
    {
        public ModelConfig() { }

        public string GUID { get; set; }
        public string TableName { get; set; }
        public string TableDescription { get; set; }
        public string ModelTypeID { get; set; }
        public string DefaultView { get; set; }
        public string Type { get; set; }
        public string TypeName { get; set; }
        public string AddType { get; set; }
        public string ParentModelID { get; set; }
        public string IsRecord { get; set; }
        public int Step { get; set; }
        public int Status { get; set; }
        public int Version { get; set; }
        public string count { get; set; }
        public string SchemaName { get; set; }
        public string ModelTableCode { get; set; }
        public string TableType { get; set; }
        public string vwName { get; set; }
        private DateTime _CreateTime;
        public string CreateTime
        {
            get
            {
                return _CreateTime.ToString("yyyy-MM-dd");
            }
            set
            {
                _CreateTime = Convert.ToDateTime(value);
            }
        }
        private DateTime _VersionTime;
        public string VersionTime
        {
            get
            {
                return _VersionTime.ToString("yyyy-MM-dd HH:mm:ss");
            }
            set
            {
                _VersionTime = Convert.ToDateTime(value);
            }
        }
    }

    public class ModelViewConfig
    {
        public ModelViewConfig() { }
        //------- model

        public string SourceSysName { get; set; }
        public string ColumnSourceID { get; set; }
        public string TableName { get; set; }
        private string _FactColumnName = string.Empty;
        public string FactColumnName
        {
            get
            {
                //if (!string.IsNullOrEmpty(_FactColumnName))
                //{
                //    string specialStr = "|!@#$%^&*|";
                //    if (_FactColumnName.Contains(specialStr))
                //    {
                //        int specialStrIndex = _FactColumnName.IndexOf(specialStr);
                //        return _FactColumnName.Substring(specialStrIndex + specialStr.Length);
                //    }
                //    return _FactColumnName;
                //}
                //else
                //{
                //    return _FactColumnName;
                //}
                //if (_FactColumnName.Length > 32)
                //    return _FactColumnName.Substring(32);
                //else
                    return _FactColumnName;
            }
            set
            {
                _FactColumnName = value;
            }
        }
        //------- view
        public string GUID { get; set; }
        public string ModelTableID { get; set; }
        private string _columnName = string.Empty;
        public string ColumnName
        {
            get
            {
                if (!string.IsNullOrEmpty(_columnName))
                {
                    string specialStr = "|!@#$%^&*|";
                    if (_columnName.Contains(specialStr))
                    {
                        int specialStrIndex = _columnName.IndexOf(specialStr);
                        return _columnName.Substring(specialStrIndex + specialStr.Length);
                    }
                    return _columnName;
                }
                else
                {
                    return _columnName;
                }
            }
            set
            {
                _columnName = value;
            }
        }
        private string _ColumnDescription;
        public string ColumnDescription
        {
            get
            {
                return string.IsNullOrEmpty(_ColumnDescription) ? "" : _ColumnDescription;
            }
            set
            {
                _ColumnDescription = value;
            }
        }
        private string _columnType = string.Empty;
        public string ColumnType
        {
            get
            {
                return FomatColumnType(_columnType.ToLower(), this.ColumnLength.ToString(), this.ColumnPrecision.ToString(), this.ColumnScale.ToString());
            }
            set
            {
                _columnType = value;
            }
        }
        public int ColumnIsModel
        {
            get;
            set;
        }
        public int ColumnIsBusinessPK
        {
            get;
            set;
        }
        public int ColumnLength { get; set; }
        public int ColumnPrecision { get; set; }
        public int ColumnScale { get; set; }
        private string _ColumnIsNullable;
        public string ColumnIsNullable
        {
            get
            {
                return string.IsNullOrEmpty(_ColumnIsNullable) ? "" : _ColumnIsNullable;
            }
            set
            {
                _ColumnIsNullable = value;
            }
        }
        private string _PK;
        public string PK
        {
            get
            {
                if (string.IsNullOrEmpty(_PK))
                    _PK = "";
                return _PK.Trim() == "1" || _PK.Trim() == "是" ? "是" : "否";
            }
            set { _PK = value; }
        }
        private string _ColumnDefaultValue;
        public string ColumnDefaultValue
        {
            get
            {
                return string.IsNullOrEmpty(_ColumnDefaultValue) ? "" : _ColumnDefaultValue;
            }
            set
            {
                _ColumnDefaultValue = value;
            }
        }
        private string _ColumnFixValue;
        public string ColumnFixValue
        {
            get
            {
                return string.IsNullOrEmpty(_ColumnFixValue) ? "" : _ColumnFixValue;
            }
            set
            {
                _ColumnFixValue = value;
            }
        }
        public string WhereCondition { get; set; }
        public string Operator { get; set; }
        public string ColumnExpression { get; set; }
        public string ColumnScriptExtend { get; set; }
        public string IsValue { get; set; }
        public string ColumnOrder { get; set; }
        private string FomatColumnType(string columnType, string columnLength, string columnPrecision, string decimalPlaces)
        {
            string result = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(columnType))
                {
                    if (columnType.Equals("binary") || columnType.Equals("char") || columnType.Equals("datetime2") || columnType.Equals("datetimeoffset") || columnType.Equals("nchar")
                        || columnType.Equals("time") || columnType.Equals("varbinary") || columnType.Equals("varchar"))
                    {
                        result = string.Format("{0}({1})", columnType, columnLength);
                    }
                    else if (columnType.Equals("nvarchar"))
                    {
                        if (columnLength.Equals("-1"))
                        {
                            result = "nvarchar(max)";
                        }
                        else
                        {
                            result = string.Format("{0}({1})", columnType, columnLength);
                        }
                    }
                    else if (columnType.Equals("uniqueidentifier"))
                    {
                        result = "nvarchar(max)";
                    }
                    else if (columnType.Equals("numeric") || columnType.Equals("decimal"))
                    {
                        result = string.Format("{0}({1},{2})", columnType, columnPrecision, decimalPlaces);
                    }
                    else
                    {
                        result = columnType;
                    }
                }
                else
                {
                    result = "";
                }
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

    }
}
